//
//  ServiceCharge.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/5.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "ServiceCharge.h"
#import <Masonry.h>


#define WIDTH  [[UIScreen mainScreen]bounds].size.width

#define HEIGHT [[UIScreen mainScreen]bounds].size.height

@implementation ServiceCharge

-(UIView *)initWithFrame:(CGRect )frame andType:(NSInteger)viewType AndModel:(MineHelpCenterRecord *)record
{
    if (self = [super initWithFrame:frame]) {
        if (viewType == 0) {
            //服务费标准
            [self setupViewsWithModel:record];
        }else if (viewType == 1)
        {
            //刷卡操作说明
            [self swipeCard];
        }else if (viewType == 2)
        {
            //刷卡到账说明
//            [self accountingDate];
            [self setupViewsWithModel:record];
        }
        
    }
    return self;
}

-(void)setupViewsWithModel:(MineHelpCenterRecord *)record{
    self.backgroundColor = [UIColor clearColor];
    
    
    self.backgroundView = [[UIView alloc] initWithFrame:(CGRectMake(0, HEIGHT - 400, WIDTH, 400))];
    self.backgroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backgroundView];
    
    
    self.topView = [[UIView alloc] init];
    [self.backgroundView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:229 / 255.0 alpha:1];
    [self.topView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    self.ServiceChargeLab = [[UILabel alloc] init];
    self.ServiceChargeLab.text = record.conTitle;
    self.ServiceChargeLab.font = [UIFont boldSystemFontOfSize:16];
    self.ServiceChargeLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [self.topView addSubview:self.ServiceChargeLab];
    [self.ServiceChargeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.topView);
    }];
    
    self.offBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.offBtn setImage:[UIImage imageNamed:@"icon_off"] forState:(UIControlStateNormal)];
    [self.topView addSubview:self.offBtn];
    [self.offBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(self.topView);
    }];
    
  

    //根据竖线分割字符串
    NSString *contentStr = record.content;
    if ([contentStr containsString:@"|"]) {      //根据@"|"截取行数     根据@":"截取字体
        //分割字符串,创建Label
        NSArray *labelArray = [contentStr componentsSeparatedByString:@"|"];
        for (int i = 0; i < labelArray.count; i++) {
            UILabel *lab = [[UILabel alloc] init];
            lab.numberOfLines = 0;
            NSString *content = labelArray[i];
            if ([content containsString:@"："]) {
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];
                [attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, [self getStrRange:content].location)];
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(0, [self getStrRange:content].location)];
                
                
                [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange([self getStrRange:content].location, content.length - [self getStrRange:content].location)];
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange([self getStrRange:content].location, content.length - [self getStrRange:content].location)];
                lab.attributedText = attributedString;
            }else
            {
                lab.textColor = ETColorWithRGB(51, 51, 51);
                lab.font = [UIFont systemFontOfSize:14];
                lab.text = content;
            }
            lab.tag = 20000 + i;
            [self.backgroundView addSubview:lab];
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.right.mas_equalTo(-15);
                if (i == 0 ) {
                    make.top.equalTo(self.topView.mas_bottom).offset(20);
                }else
                {
                    make.top.equalTo([self viewWithTag:20000 + i - 1].mas_bottom).offset(20);
                }
            }];
        }
    }else
    {
        UILabel *lab = [[UILabel alloc] init];
        lab.numberOfLines = 0;
        lab.textColor = ETColorWithRGB(51, 51, 51);
        lab.font = [UIFont systemFontOfSize:14];
        lab.text = record.content;
        [self.backgroundView addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.equalTo(self.topView.mas_bottom).offset(20);
        }];
    }
    
    
    
}



-(NSRange )getStrRange:(NSString *)str
{
    NSString *tmpStr = str;
    
    NSRange range;
    
    range = [tmpStr rangeOfString:@"："];
    
    if (range.location != NSNotFound) {
        
        NSLog(@"found at location = %lu, length = %lu",(unsigned long)range.location,(unsigned long)range.length);
        
        
        return range;
        
    }else{
        return range;
        NSLog(@"Not Found");
        
    }
}



-(void)swipeCard
{
    self.backgroundColor = [UIColor clearColor];
    
    self.backgroundView = [[UIView alloc] initWithFrame:(CGRectMake(0, HEIGHT - 400, WIDTH, 400))];
    self.backgroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backgroundView];
    
    
    self.topView = [[UIView alloc] init];
    [self.backgroundView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:229 / 255.0 alpha:1];
    [self.topView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    self.ServiceChargeLab = [[UILabel alloc] init];
    self.ServiceChargeLab.text = @"刷卡操作说明";
    self.ServiceChargeLab.font = [UIFont boldSystemFontOfSize:16];
    self.ServiceChargeLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [self.topView addSubview:self.ServiceChargeLab];
    [self.ServiceChargeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.topView);
    }];
    
    self.offBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.offBtn setImage:[UIImage imageNamed:@"icon_off"] forState:(UIControlStateNormal)];
    [self.topView addSubview:self.offBtn];
    [self.offBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(self.topView);
    }];
    
    self.chipCardImv = [[UIImageView alloc] init];
    self.chipCardImv.image = [UIImage imageNamed:@"help_icon_xinpianka"];
    [self.backgroundView addSubview:self.chipCardImv];
    [self.chipCardImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-35);
        make.top.equalTo(self.lineView).offset(30);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(130);
    }];
    
    
    self.haveChipLab = [[UILabel alloc] init];
    self.haveChipLab.text = @"正面有芯片";
    self.haveChipLab.textColor = [UIColor colorWithRed:91 / 255.0 green:113 / 255.0 blue:255 / 255.0 alpha:1];
    self.haveChipLab.font = [UIFont boldSystemFontOfSize:14];
    [self.backgroundView addSubview:self.haveChipLab];
    [self.haveChipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(55);
        make.centerY.equalTo(self.chipCardImv);
        
    }];
    
    
    self.chipCardLab = [[UILabel alloc] init];
    self.chipCardLab.text = @"芯片卡";
    self.chipCardLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    self.chipCardLab.font = [UIFont boldSystemFontOfSize:18];
    [self.backgroundView addSubview:self.chipCardLab];
    [self.chipCardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.haveChipLab).offset(5);
        make.bottom.equalTo(self.haveChipLab.mas_top).offset(-10);
    }];
    
    self.chipCardTipsLab = [[UILabel alloc] init];
    self.chipCardTipsLab.text = @"将芯片卡插入卡槽中";
    self.chipCardTipsLab.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    self.chipCardTipsLab.font = [UIFont systemFontOfSize:14];
    [self.backgroundView addSubview:self.chipCardTipsLab];
    [self.chipCardTipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.haveChipLab);
        make.top.equalTo(self.haveChipLab.mas_bottom).offset(10);
    }];
    
    
    self.magneticStripeImv = [[UIImageView alloc] init];
    self.magneticStripeImv.image = [UIImage imageNamed:@"help_icon_citiaoka"];
    [self.backgroundView addSubview:self.magneticStripeImv];
    [self.magneticStripeImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.chipCardTipsLab);
        make.top.equalTo(self.chipCardImv.mas_bottom).offset(20);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(130);
    }];
    
    
    
    self.noChipLab = [[UILabel alloc] init];
    self.noChipLab.text = @"正面无芯片";
    self.noChipLab.textColor = [UIColor colorWithRed:91 / 255.0 green:113 / 255.0 blue:255 / 255.0 alpha:1];
    self.noChipLab.font = [UIFont boldSystemFontOfSize:14];
    [self.backgroundView addSubview:self.noChipLab];
    [self.noChipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-55);
        make.centerY.equalTo(self.magneticStripeImv).offset(-10);
        
    }];
    
    
    self.magneticStripeCardLab = [[UILabel alloc] init];
    self.magneticStripeCardLab.text = @"磁条卡";
    self.magneticStripeCardLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    self.magneticStripeCardLab.font = [UIFont boldSystemFontOfSize:18];
    [self.backgroundView addSubview:self.magneticStripeCardLab];
    [self.magneticStripeCardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.noChipLab).offset(5);
        make.bottom.equalTo(self.noChipLab.mas_top).offset(-10);
    }];
    
    self.magneticStripeCardTipsLab = [[UILabel alloc] init];
    self.magneticStripeCardTipsLab.text = @"磁条朝下\n从左到右匀速刷卡";
    self.magneticStripeCardTipsLab.numberOfLines = 0;
    self.magneticStripeCardTipsLab.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    self.magneticStripeCardTipsLab.font = [UIFont systemFontOfSize:14];
    [self.backgroundView addSubview:self.magneticStripeCardTipsLab];
    [self.magneticStripeCardTipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.noChipLab);
        make.top.equalTo(self.noChipLab.mas_bottom).offset(10);
    }];
    
    self.chipCardLineImv = [[UIImageView alloc] init];
    self.chipCardLineImv.image = [UIImage imageNamed:@"icon_line"];
    [self.backgroundView addSubview:self.chipCardLineImv];
    [self.chipCardLineImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.chipCardLab.mas_left).offset(-5);
        make.centerY.equalTo(self.chipCardLab);
        make.width.mas_equalTo(3);
        make.height.mas_equalTo(12);
    }];
    
    self.magneticStripeImv = [[UIImageView alloc] init];
    self.magneticStripeImv.image = [UIImage imageNamed:@"icon_line"];
    [self.backgroundView addSubview:self.magneticStripeImv];
    [self.magneticStripeImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.magneticStripeCardLab.mas_right).offset(5);
        make.centerY.equalTo(self.magneticStripeCardLab);
        make.width.mas_equalTo(3);
        make.height.mas_equalTo(12);
    }];
}

-(void)accountingDate
{
    self.backgroundColor = [UIColor clearColor];
    
    self.backgroundView = [[UIView alloc] initWithFrame:(CGRectMake(0, HEIGHT - 400, WIDTH, 400))];
    self.backgroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backgroundView];
    
    self.topView = [[UIView alloc] init];
    [self.backgroundView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor colorWithRed:229 / 255.0 green:229 / 255.0 blue:229 / 255.0 alpha:1];
    [self.topView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    self.ServiceChargeLab = [[UILabel alloc] init];
    self.ServiceChargeLab.text = @"到账时间说明";
    self.ServiceChargeLab.font = [UIFont boldSystemFontOfSize:16];
    self.ServiceChargeLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [self.topView addSubview:self.ServiceChargeLab];
    [self.ServiceChargeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.equalTo(self.topView);
    }];
    
    self.offBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.offBtn setImage:[UIImage imageNamed:@"icon_off"] forState:(UIControlStateNormal)];
    [self.topView addSubview:self.offBtn];
    [self.offBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(self.topView);
    }];
    
    self.morrowLab = [[UILabel alloc] init];
    NSMutableAttributedString *mrrowAttributedString = [[NSMutableAttributedString alloc] initWithString:@"次日到账:第二个工作日到账"];
    [mrrowAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, 5)];
    [mrrowAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 5)];
    
    
    [mrrowAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(5, 8)];
    [mrrowAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(5, 8)];
    self.morrowLab.attributedText = mrrowAttributedString;
    self.morrowLab.numberOfLines = 0;
    [self.backgroundView addSubview:self.morrowLab];
    [self.morrowLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self.lineView.mas_bottom).offset(20);
    }];
    
    
    self.todayLab = [[UILabel alloc] init];
    NSMutableAttributedString *todayAttributedString = [[NSMutableAttributedString alloc] initWithString:@"立即到账:5分钟内到账,24小时可用"];
    [todayAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, 5)];
    [todayAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(0, 5)];
    
    
    [todayAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(5, 13)];
    [todayAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] range:NSMakeRange(5, 13)];
    self.todayLab.attributedText = todayAttributedString;
    self.todayLab.numberOfLines = 0;
    [self.backgroundView addSubview:self.todayLab];
    [self.todayLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self.morrowLab.mas_bottom).offset(20);
    }];
}

@end
